package P6.T02;

import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.TextAction;
import java.awt.*;
import java.awt.event.*;

public class JMyFrameP6 extends JFrame {
    private JLabel lbl=new JLabel("");
    private JTextField txt=new JTextField();
    private JButton btn=new JButton("t");

    public JMyFrameP6() {
        this.setTitle("My Frame P6");
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        add(lbl, BorderLayout.NORTH);
        add(txt, BorderLayout.SOUTH);
        add(btn,BorderLayout.EAST);
        btn.addActionListener(new MyActionListener());

        this.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                lbl.setText("按下了"+e.getKeyChar());
            }

            @Override
            public void keyReleased(KeyEvent e) {
                lbl.setText("抬起了"+e.getKeyChar());
            }
        });
        addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                lbl.setText("老鼠来啦，好怕怕");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                lbl.setText("老鼠终于走啦");
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                lbl.setText("老鼠在"+e.getX()+","+e.getY());
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });
        txt.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                lbl.setText("正在插入字符"+txt.getText());
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                lbl.setText("发生了删除动作"+txt.getText());
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                //lbl.setText("修改触发了");
            }
        });
//        txt.addCaretListener(new CaretListener() {
//            @Override
//            public void caretUpdate(CaretEvent e) {
//                lbl.setText("文本被修改了"+e.getDot()+"-"+e.getMark());
//            }
//        });
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

